<template>
  <div>
    <el-form ref="form" :model="form" label-width="120px" style="width: 650px">
      <el-form-item label="习题驾照类型" prop="licensetype">
        <el-checkbox-group v-model="form.licensetype">
          <el-checkbox
            v-for="item in licensetypes"
            :key="item.license_type_id"
            :label="item.license_type"
            :value="item.license_type"
          ></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="习题科目" prop="subjectstype">
        <el-checkbox-group v-model="form.subjectstype">
          <el-checkbox
            v-for="item in subjectstypes"
            :key="item.subjects_id"
            :label="item.subjects_type"
            :value="item.subjects_type"
          ></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="习题照片" prop="exercise_cover">
        <el-upload
          class="avatar-uploader"
          :action="`${UPLOADURL}/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :limit="1"
        >
          <img
            v-if="form.exercise_cover"
            :src="form.exercise_cover"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="习题内容" prop="exercise_text">
        <el-input type="text" v-model="form.exercise_text"></el-input>
      </el-form-item>
      <el-form-item label="习题选项" prop="exercise_options">
        <el-input type="text" v-model="form.exercise_options"></el-input>
      </el-form-item>
      <el-form-item label="习题答案" prop="exercise_answers">
        <el-input type="text" v-model="form.exercise_answers"></el-input>
      </el-form-item>
      <el-form-item label="习题解析" prop="exercise_resolve">
        <el-input
          v-model="form.exercise_resolve"
          type="textarea"
          autosize
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updatexercise">修改习题</el-button>
        <el-button @click="resetform">重置</el-button>
        <el-button @click="cancel">取消返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import httpApi from "@/http";

export default {
  data() {
    return {
      subjectstypes: [],
      licensetypes: [],

      form: {
        exercise_id: this.$route.params.id,
        exercise_cover: "",
        exercise_text: "",
        exercise_options: "",
        exercise_answers: "",
        exercise_resolve: "",
        subjectstype: [],
        licensetype: [],
      },
    };
  },
  methods: {
    resetform() {
      (this.form.exercise_cover = ""),
        (this.form.exercise_cover = ""),
        (this.form.exercise_text = ""),
        (this.form.exercise_options = ""),
        (this.form.exercise_answers = ""),
        (this.form.exercise_resolve = ""),
        (this.form.subjectstype = []),
        (this.form.licensetype = []);
    },
    cancel() {
      this.$router.push("/home/exercises-list");
    },

    updatexercise() {
      this.form.licensetype = this.form.licensetype.join("/");
      this.form.subjectstype = this.form.subjectstype.join("/");
      this.$refs["form"].validate((valid) => {
        if (valid) {
          httpApi.exerciseApi.update(this.form).then((res) => {
            console.log("习题添加", res);
            if (res.data.code == 200) {
              this.$message.success("练习添加成功");
              this.$router.push("/home/exercises-list");
            } else {
              this.$message.error("添加失败请检查");
              this.form.licensetype = this.form.licensetype.split("/");
              this.form.subjectstype = this.form.subjectstype.split("/");
            }
          });
        }
      });
    },
    // 图片上传处理
    handleAvatarSuccess(res, file) {
      console.log(res, file);
      this.form.exercise_cover = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传驾校照片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传驾校照片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    infolicensetype() {
      httpApi.commpleApi.queryLicensetype().then((res) => {
        console.log("驾照类型标签", res);
        this.licensetypes = res.data.data.result;
      });
    },
    infosubjiettype() {
      httpApi.commpleApi.querySubjectstype().then((res) => {
        console.log("科目类型标签", res);
        this.subjectstypes = res.data.data.result;
      });
    },
    queryById() {
      let params = {
        exercise_id: this.form.exercise_id,
      };
      httpApi.exerciseApi.queryById(params).then((res) => {
        console.log("加载习题信息", res);
        this.form = res.data.data.result[0];
        this.form.licensetype = this.form.licensetype.split("/");
        this.form.subjectstype = this.form.subjectstype.split("/");
      });
    },
  },
  mounted() {
    this.infolicensetype();
    this.infosubjiettype();
    this.queryById();
  },
};
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
</style>